<template>
  <div class="login_container">
    <!-- 标题 -->
    <div class="title">
      后台管理系统
    </div>
    <div class="login_box">
      <!-- 登录表单区 -->
      <el-form ref="loginForm" :model="loginForm" :rules="loginFormRules" label-width="0px">
        <!-- 用户名 -->
        <el-form-item class="username" prop="username">
          <el-input v-model="loginForm.username" class="username_1" placeholder="用户名 / 邮箱" clearable></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item class="password" prop="password">
          <el-input v-model="loginForm.password" show-password placeholder="密码"></el-input>
        </el-form-item>

        <!-- 按钮区域 -->
        <el-form-item class="btn">
          <el-button type="primary" @click="login" class="btn_1">登录</el-button>
        </el-form-item>

      </el-form>

    </div>
  </div>
</template>

<script>
export default {
  name: '',
  data () {
    return {
      loginForm: {
        username: '1846330313',
        password: 'dws123456...'
      },
      loginFormRules: {
        username: [
          { require: true, message: '请输入用户名', trigger: 'blur' },

          { min: 3, max: 10, message: '长度在3到10个字符之间', trigger: 'blur' }
        ],
        password: [
          { require: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    login () {
      this.$refs.loginForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('api/user/login', JSON.stringify(this.loginForm))
        console.log(res)
        if (res.code !== 200) return this.$message.error(res.msg)
        this.$message.success(res.msg)
        // 1.将登录成功后的token，保存到客户端的sessionStorage中
        //  1.1 项目中除了登录和注册，找回密码之外其他的api接口必须登录之后才能访问
        //  1.2因为是后台系统，token只应该在网站打开期间生效，所以将token保存到sessionStorage中
        window.sessionStorage.setItem('token', res.token)
        window.sessionStorage.setItem('permissions', JSON.stringify(res.permissions))
        // 2.通过编程式导航跳转到后台主页。路由地址是/home
        this.$router.push('/home')
      })
    }
  }
}
</script>

<style lang="less">
.login_container {
  background-color: #2d3a4b;
  height: 100%;
  .title {
    font-size: 40px;
    color: #4396ec;
    position: absolute;
    top: 22%;
    left: 43%;
  }
}
.login_box {
  width: 450px;
  height: 300px;
  // background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 37%;
  top: 30%;
  .username {
    padding: 15px 10px;
  }
  .password {
    padding: 0 10px;
  }
}
.btn {
  padding: 10px 10px;
  .btn_1 {
    display: block;
    width: 100%;
    border-radius: 6px;
    font-size: 15px;
  }
}
/deep/.el-input__inner {
  background-color: #283443 !important;
  border: 1px solid #889aa4;
  color: #fff;
}
</style>
